/**
  ******************************************************************************
  * @文件名           : dri_DigitalQuantity.c
  * @作者             : citunhk
  * @功能介绍         : HMI通讯接收，以及相应的命令处理及返回
  *	@日期             : 2022.11.1
  ******************************************************************************
  * @注意
  *
  *
  *
  ******************************************************************************
  *更改记录
  *日期 修改人 修改内容
  *
  *
  *
  ******************************************************************************
  */
#include "dri_DigitalQuantity.h"
#include "bsp_gpio.h"
#include "sys_data.h"

/* 定义输入信号并初始化 */
Equ_DigitaIn_ConfigStruct Equ_DI_Struct[8] ={
		 {Photocoupler_DGT1_Port, Photocoupler_DGT1_Pin, LOW_LEVEL_FAULT, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT2_Port, Photocoupler_DGT2_Pin, OUTPUT_LOW_FLOW, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT3_Port, Photocoupler_DGT3_Pin, INPUT_LOW_FLOW, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT4_Port, Photocoupler_DGT4_Pin, RE_HIGHT_PRESS, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT5_Port, Photocoupler_DGT5_Pin, RE_LOW_PRESS, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT6_Port, Photocoupler_DGT6_Pin, IN_DISABLE, NC, LEVEL, 2, 0},
		 {Photocoupler_DGT7_Port, Photocoupler_DGT7_Pin, IN_DISABLE, NC, LEVEL, 2, 0}
};
/* 定义输出信号并初始化 */
Equ_DigitaOut_ConfigStruct Equ_DO_Struct[10] = {
		{LED_Green_Port, LED_Green_Pin, LED_RUN, NO, OFF},/* 0 */
		{LED_Red_Port, LED_Red_Pin, LED_ALARM, NO, OFF},/* 1 */
		{BUZZER_Port, BUZZER_Pin, BUZZER1, NC, OFF},/* 2 */
		{Silicon_U1_Port, Silicon_U1_Pin, RE_VALVE, NO, OFF},/* 3 */
		{Silicon_U2_Port, Silicon_U2_Pin, HEAT, NC, OFF},/* 4 */
		{Silicon_U3_Port, Silicon_U3_Pin, PUMP, NC, OFF},/* 5 */
		{Silicon_U4_Port, Silicon_U4_Pin, COMP, NC, OFF},/* 6 */
		{Relays_K1_Port, Relays_K1_Pin, OUT_DISABLE, NC, OFF},/* 7 */
		{Relays_K2_Port, Relays_K2_Pin, OUT_DISABLE, NC, OFF},/* 8 */
		{Relays_K3_Port, Relays_K3_Pin, OUT_DISABLE, NC, OFF}/* 9 */
};

//Equ_TypeDef EQU_LED_RUN = {LED_Green_Port, LED_Green_Pin, NO};     //运行指示灯
//Equ_TypeDef EQU_LED_ALARM = {LED_Red_Port, LED_Red_Pin, NO};       //报警指示灯
//Equ_TypeDef EQU_BUZZER = {BUZZER_Port, BUZZER_Pin, NC};            //蜂鸣器

//Equ_TypeDef EQU_LEVEL = {Photocoupler_DGT1_Port, Photocoupler_DGT1_Pin, NC};  //低液位
//Equ_TypeDef EQU_CHW = {Photocoupler_DGT2_Port, Photocoupler_DGT2_Pin, NC};  //冷冻水低流量
//Equ_TypeDef EQU_COW = {Photocoupler_DGT3_Port, Photocoupler_DGT3_Pin, NC};  //冷却水低流量
//Equ_TypeDef EQU_ReHighPress = {Photocoupler_DGT4_Port, Photocoupler_DGT4_Pin, NC};  //氟路高压
//Equ_TypeDef EQU_ReLowPress = {Photocoupler_DGT5_Port, Photocoupler_DGT5_Pin, NC};  //氟路低压
//Equ_TypeDef EQU_IN6 = {Photocoupler_DGT6_Port, Photocoupler_DGT6_Pin, NC};  //自定义输入1
//Equ_TypeDef EQU_IN7 = {Photocoupler_DGT7_Port, Photocoupler_DGT7_Pin, NC};  //自定义输入2

//Equ_TypeDef EQU_SPARE = {Relays_K1_Port, Relays_K1_Pin, NC};                //气吹回液
//Equ_TypeDef EQU_ALARM= {Relays_K2_Port, Relays_K2_Pin, NC};                  //报警输出
//Equ_TypeDef EQU_VALVE_HIGH = {Relays_K3_Port, Relays_K3_Pin, NC};           //高温制冷阀
//Equ_TypeDef EQU_VALVE_PID = {Silicon_U1_Port, Silicon_U1_Pin, NO};            //PID控制阀
////Equ_TypeDef EQU_VALVE_PID = {Silicon_U1_Port, Silicon_U1_Pin, NC};            //PID控制阀
//Equ_TypeDef EQU_HEAT = {Silicon_U2_Port, Silicon_U2_Pin, NC};                 //加热
//Equ_TypeDef EQU_PUMP = {Silicon_U3_Port, Silicon_U3_Pin, NC};                 //循环泵
//Equ_TypeDef EQU_COMP = {Silicon_U4_Port, Silicon_U4_Pin, NC};                 //压缩机

//DigitalQuantityInType_t EQU_TYPE_IN6;  //用户自定义输入1-DIN6类型
//DigitalQuantityInType_t EQU_TYPE_IN7;  //用户自定义输入2-DIN7类型
//
//DigitalQuantityInDefinition_t EQU_DEFINITION_IN6;  //用户自定义输入1-DIN6定义
//DigitalQuantityInDefinition_t EQU_DEFINITION_IN7;  //用户自定义输入2-DIN7定义
//DigitalQuantity4OutDefinition_t EQU_DEFINITION_IN4;  //工厂自定义输入1-DIN4定义
//DigitalQuantity5OutDefinition_t EQU_DEFINITION_IN5;  //工厂自定义输入2-DIN5定义
//DigitalQuantity2OutDefinition_t EQU_DEFINITION_IN2;  //工厂自定义输入3-DIN2定义
//DigitalQuantity7OutDefinition_t EQU_DEFINITION_IN27;  //工厂自定义输入4-DIN7定义
//
//DigitalQuantityOutDefinition_t EQU_DEFINITION_OUT1;  //用户自定义输出1-KA1定义
//DigitalQuantityOutDefinition_t EQU_DEFINITION_OUT2;  //用户自定义输出2-KA2定义
//DigitalQuantity12OutDefinition_t EQU_DEFINITION_OUT3;  //工厂自定义输出1-KA3定义
//DigitalQuantity12OutDefinition_t EQU_DEFINITION_22;  //工厂自定义输出2-KA2定义
//
//AlarmOutDefinition_t EQU_ALARM_DEFINITION_OUT1;  //用户自定义输出1-KA1报警配置
//AlarmOutDefinition_t EQU_ALARM_DEFINITION_OUT2;  //用户自定义输出2-KA2报警配置
//
//MaintainOutDefinition_t EQU_MAINTAIN_DEFINITION_OUT1;  //用户自定义输出1-KA1维护提示配置
//MaintainOutDefinition_t EQU_MAINTAIN_DEFINITION_OUT2;  //用户自定义输出2-KA2维护提示配置

/**
* @函数名	DRI_EQU_GetFunStatePinState
* @功能	    获取信号输入状态函数
* @入口参数	EQU_Name 外设结构体地址
* @返回值	equState 外设输入状态返回值 OFF：未动作
*                                ON：动作
*/
equState_t DRI_EQU_GetPinState(Equ_DigitaIn_ConfigStruct EQU_Name)
{
	uint8_t stateInput = 0;

	stateInput = BSP_GPIO_InputRead(EQU_Name.GPIOx, EQU_Name.PinMask);

	if((EQU_Name.NormalState == NC && stateInput==1) || (EQU_Name.NormalState == NO && stateInput==0))
		return ON;
	else //((EQU_Name->NormalState == NC && stateInput==0) || (EQU_Name->NormalState == NO && stateInput==1))
		return OFF;
}

/**
* @函数名	DRI_EQU_SetFunStatePinState
* @功能	    外设输出驱动函数
* @入口参数	EQU_Name 外设结构体地址
*           equState 外设驱动状态 OFF
*                                ON
*                                TOGGLE
* @返回值	无
*/
void DRI_EQU_SetPinState(Equ_DigitaOut_ConfigStruct EQU_Name,equState_t equState)
{
	if( (equState == ON && EQU_Name.NormalState == NC )\
			|| (equState == OFF && EQU_Name.NormalState == NO) )
		{
			BSP_GPIO_OutputOperate(EQU_Name.GPIOx, EQU_Name.PinMask, PIN_SET);
		}
		else if( (equState == ON && EQU_Name.NormalState == NO )\
				|| (equState == OFF && EQU_Name.NormalState == NC) )
		{
			BSP_GPIO_OutputOperate(EQU_Name.GPIOx, EQU_Name.PinMask, PIN_RESET);
		}
		else if(equState == TOGGLE)
		{
			BSP_GPIO_OutputOperate(EQU_Name.GPIOx, EQU_Name.PinMask, PIN_TOGGLE);
		}
}
/**
* @函数名	DRI_EQU_SetFunState
* @功能	    根据控制的功能，设置对应管脚输出状态
* @入口参数	outType 要控制的功能
*           state 设置的状态
* @返回值	无
*/
void DRI_EQU_SetFunState(digitalOutType_t outType, equState_t state)
{
	for(uint8_t i = 0; i < 10; i++)
	{
		if(Equ_DO_Struct[i].SignalType == outType && state == TOGGLE)
		{
			DRI_EQU_SetPinState(Equ_DO_Struct[i], state);
			Equ_DO_Struct[i].EquState = state;
		}
		else  if(Equ_DO_Struct[i].SignalType == outType && Equ_DO_Struct[i].EquState != state)
		{
			DRI_EQU_SetPinState(Equ_DO_Struct[i], state);
			Equ_DO_Struct[i].EquState = state;
		}
	}
}

/**
* @函数名	DRI_EQU_GetFunState
* @功能	    根据控制的功能，获取信号输入状态函数
* @入口参数	outType 外设结构体地址
* @返回值	equState 外设输入状态返回值 OFF：未动作
*                                ON：动作
*/
equState_t DRI_EQU_GetFunState(digitalInType_t inType)
{
	for(uint8_t i = 0; i < 8; i++)
	{
		if(Equ_DI_Struct[i].SignalType == inType)
			return DRI_EQU_GetPinState(Equ_DI_Struct[i]);
	}
	return STATE_NO_CONFIG;//未配置输入的功能
}

triggerType_t DRI_EQU_GetFunTrigger(digitalInType_t inType)
{
	for(uint8_t i = 0; i < 8; i++)
	{
		if(Equ_DI_Struct[i].SignalType == inType)
			return Equ_DI_Struct[i].TriggerType;
	}
	return TRIGGER_NO_CONFIG;//未配置输入的功能
}

void DRI_EQU_UserConfig_Output1(void)
{
	//用户数字量输出1-KA1 连接类型
	if(UP.Para.Digital_IO_Dispose & 0x0004)  //常闭
	{
		Equ_DO_Struct[K1].NormalState = NO;
	}
	else  //常开
	{
		Equ_DO_Struct[K1].NormalState = NC;
	}
	//用户数字量输出1-KA1 信号定义
	switch(UP.Para.Digital_Out1_Definition)  //输出1定义
	{
	case 0:
		Equ_DO_Struct[K1].SignalType = OUT_DISABLE;
		break;
	case 1:
		Equ_DO_Struct[K1].SignalType = RUN_STATE_OUT;
		break;
	case 2:
		Equ_DO_Struct[K1].SignalType = DIGITAL_STATE_OUT;
		break;
	case 3:
		Equ_DO_Struct[K1].SignalType = ALARM_OUT;
		break;
	case 4:
		Equ_DO_Struct[K1].SignalType = WARNINGS_OUT;
		break;
	case 5:
		Equ_DO_Struct[K1].SignalType = ALL_ALARM_OUT;
		break;
	case 6:
		Equ_DO_Struct[K1].SignalType = ALL_MAINTAIN_OUT;
		break;
	case 7:
		Equ_DO_Struct[K1].SignalType = SELECT_ALARM_OUT;  //选择的报警
		break;
	case 8:
		Equ_DO_Struct[K1].SignalType = SELECT_MAINTAIN_OUT;  //选择的维护提示
		break;
	default:
		Equ_DO_Struct[K1].SignalType = OUT_DISABLE;
		break;
	}
}
void DRI_EQU_UserConfig_Output2(void)
{
	//用户数字量输出2-KA2 连接类型
	if(UP.Para.Digital_IO_Dispose & 0x0008)  //常闭
	{
		Equ_DO_Struct[K2].NormalState = NO;
	}
	else  //常开
	{
		Equ_DO_Struct[K2].NormalState = NC;
	}
	//用户数字量输出2-KA2 信号定义
	switch(UP.Para.Digital_Out2_Definition)  //输出2定义
	{
	case 0:
		Equ_DO_Struct[K2].SignalType = OUT_DISABLE;  //关闭
		break;
	case 1:
		Equ_DO_Struct[K2].SignalType = RUN_STATE_OUT;
		break;
	case 2:
		Equ_DO_Struct[K2].SignalType = DIGITAL_STATE_OUT;  //数字控制状态
		break;
	case 3:
		Equ_DO_Struct[K2].SignalType = ALARM_OUT;
		break;
	case 4:
		Equ_DO_Struct[K2].SignalType = WARNINGS_OUT;
		break;
	case 5:
		Equ_DO_Struct[K2].SignalType = ALL_ALARM_OUT;
		break;
	case 6:
		Equ_DO_Struct[K2].SignalType = ALL_MAINTAIN_OUT;
		break;
	case 7:
		Equ_DO_Struct[K2].SignalType = SELECT_ALARM_OUT;  //选择的报警
		break;
	case 8:
		Equ_DO_Struct[K2].SignalType = SELECT_MAINTAIN_OUT;  //选择的维护提示
		break;
	default:
		Equ_DO_Struct[K2].SignalType = OUT_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_UserConfig_Input1(void)
{
	//用户数字量输入1-DIN6 连接类型
	if(UP.Para.Digital_IO_Dispose & 0x0100)  //常闭
	{
		Equ_DI_Struct[DI6].NormalState = NC;
	}
	else  //常开
	{
		Equ_DI_Struct[DI6].NormalState = NO;
	}
	//用户数字量输入1-DIN6 信号类型
	if(UP.Para.Digital_IO_Dispose & 0x0200)  //电平
	{
		Equ_DI_Struct[DI6].TriggerType = LEVEL;
	}
	else  //边沿
	{
		Equ_DI_Struct[DI6].TriggerType = BORDER;
	}
	//用户数字量输入1-DIN6 信号定义
	switch(UP.Para.Digital_In1_Definition)  //输入1定义
	{
	case 0:
		Equ_DI_Struct[DI6].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose | 0x0200;  //边沿
		Equ_DI_Struct[DI6].TriggerType = BORDER;
		Equ_DI_Struct[DI6].SignalType = START_STOP_SWITCH;  //启停
		break;
	case 2:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose | 0x0200;  //边沿
		Equ_DI_Struct[DI6].TriggerType = BORDER;
		Equ_DI_Struct[DI6].SignalType = RESET_ALARM;  //报警复位
		break;
	case 3:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose & 0xFDFF;  //电平
		Equ_DI_Struct[DI6].TriggerType = LEVEL;
		Equ_DI_Struct[DI6].SignalType = EMER_STOP_SWITCH;  //外部保护开关
		break;
	case 4:
		Equ_DI_Struct[DI6].SignalType = CYCLE_SWITCHING;  //内外循环切换
		break;
	default:
		Equ_DI_Struct[DI6].SignalType = IN_DISABLE;  //关闭
		break;
	}
}
void DRI_EQU_UserConfig_Input2(void)
{
	//用户数字量输入2-DIN7 连接类型
	if(UP.Para.Digital_IO_Dispose & 0x0400)  //常闭
	{
		Equ_DI_Struct[DI7].TriggerType = NC;
	}
	else  //常开
	{
		Equ_DI_Struct[DI7].TriggerType = NO;
	}
	//用户数字量输入2-DIN7 信号类型
	if(UP.Para.Digital_IO_Dispose & 0x0800)  //边沿
	{
		Equ_DI_Struct[DI7].TriggerType = LEVEL;
	}
	else  //电平
	{
		Equ_DI_Struct[DI7].TriggerType = BORDER;
	}
	//用户数字量输入2-DIN7 信号定义
	switch(UP.Para.Digital_In2_Definition)  //输入2定义
	{
	case 0:
		Equ_DI_Struct[DI7].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose | 0x0200;  //边沿
		Equ_DI_Struct[DI7].TriggerType = BORDER;
		Equ_DI_Struct[DI7].SignalType = START_STOP_SWITCH;  //启停
		break;
	case 2:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose | 0x0200;  //边沿
		Equ_DI_Struct[DI7].TriggerType = BORDER;
		Equ_DI_Struct[DI7].SignalType = RESET_ALARM;  //报警复位
		break;
	case 3:
		UP.Para.Digital_IO_Dispose = UP.Para.Digital_IO_Dispose & 0xFDFF;  //电平
		Equ_DI_Struct[DI7].TriggerType = LEVEL;
		Equ_DI_Struct[DI7].SignalType = EMER_STOP_SWITCH;  //外部保护开关
		break;
	case 4:
		Equ_DI_Struct[DI7].SignalType = CYCLE_SWITCHING;  //内外循环切换
		break;
	default:
		Equ_DI_Struct[DI7].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Input1(void)
{
	//工厂数字量输入1-DIN4 信号定义
	switch(FP.Para.Digital_In1_Definition)
	{
	case 0:
		Equ_DI_Struct[DI4].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DI_Struct[DI4].SignalType = RE_HIGHT_PRESS;  //氟路高压开关
		break;
	case 2:
		Equ_DI_Struct[DI4].SignalType = OVER_LEVEL; //溢流开关
		break;
	case 3:
		Equ_DI_Struct[DI4].SignalType = RE_PRESS1; //压机1氟压开关
		break;
	case 4:
		Equ_DI_Struct[DI4].SignalType = LOW_LEVEL_ALARM; //低液位上
		break;
	default:
		Equ_DI_Struct[DI4].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Input2(void)
{
	//工厂数字量输入2-DIN5 信号定义
	switch(FP.Para.Digital_In2_Definition)
	{
	case 0:
		Equ_DI_Struct[DI5].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DI_Struct[DI5].SignalType = RE_LOW_PRESS;  //氟路低压开关
		break;
	case 2:
		Equ_DI_Struct[DI5].SignalType = EMER_STOP_SWITCH;  //急停开关
		break;
	case 3:
		Equ_DI_Struct[DI5].SignalType = RE_PRESS2;  //压机2氟压开关
		break;
	case 4:
		Equ_DI_Struct[DI5].SignalType = OVER_LEVEL;  //溢流开关
		break;
	default:
		Equ_DI_Struct[DI5].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Input3(void)
{
	//工厂数字量输入3-DIN2 信号定义
	switch(FP.Para.Digital_In3_Definition)
	{
	case 0:
		Equ_DI_Struct[DI2].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DI_Struct[DI2].SignalType = OUTPUT_LOW_FLOW;  //出液流量开关
		break;
	case 2:
		Equ_DI_Struct[DI2].SignalType = HIGHT_LEVEL; //高液位开关
		break;
	default:
		Equ_DI_Struct[DI2].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Input4(void)
{
	//工厂数字量输入4-DIN7 信号定义
	switch(FP.Para.Digital_In4_Definition)
	{
	case 0:
		Equ_DI_Struct[DI7].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DI_Struct[DI7].SignalType = OVER_LEVEL;  //溢流开关
		break;
	default:
		Equ_DI_Struct[DI7].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Output1(void)
{
	//工厂数字量输出1-KA3 信号定义
	switch(FP.Para.Digital_Out1_Definition)
	{
	case 0:
		Equ_DO_Struct[K3].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DO_Struct[K3].SignalType = HIGH_TEMP_VALVE;  //高温辅助制冷
		break;
	case 2:
		Equ_DO_Struct[K3].SignalType = LOW_TEMP_VALVE;  //低温辅助制冷
		break;
	case 3:
		Equ_DO_Struct[K3].SignalType = CYCLE_SWITCHING_VALVE;  //内外循环切换
		break;
	case 4:
		Equ_DO_Struct[K3].SignalType = RETURN_VALVE;  //气吹回液
		break;
	case 5:
		Equ_DO_Struct[K3].SignalType = DI_VALVE;  //水质电磁阀
		break;
	case 6:
		Equ_DO_Struct[K3].SignalType = RUN_STATE_OUT;  //运行状态
		break;
	default:
		Equ_DO_Struct[K3].SignalType = IN_DISABLE;  //关闭
		break;
	}
}

void DRI_EQU_FactoryConfig_Output2(void)
{
	//工厂数字量输出2-KA2 信号定义
	switch(FP.Para.Digital_Out2_Definition)
	{
	case 0:
		Equ_DO_Struct[K2].SignalType = IN_DISABLE;  //关闭
		break;
	case 1:
		Equ_DO_Struct[K2].SignalType = HIGH_TEMP_VALVE;  //高温辅助制冷
		break;
	case 2:
		Equ_DO_Struct[K2].SignalType = LOW_TEMP_VALVE;  //低温辅助制冷
		break;
	case 3:
		Equ_DO_Struct[K2].SignalType = CYCLE_SWITCHING_VALVE;  //内外循环切换
		break;
	case 4:
		Equ_DO_Struct[K2].SignalType = RETURN_VALVE;  //气吹回液
		break;
	case 5:
		Equ_DO_Struct[K2].SignalType = DI_VALVE;  //水质电磁阀
		break;
	default:
		Equ_DO_Struct[K2].SignalType = IN_DISABLE;  //关闭
		break;
	}
}
